//update products name
db.getCollection('CriteriaLevel1').update({Name:"Products"}, {$set:{Name:"Product Involvement"}},{multi:false})

var criteria = 
{ Name: "Product Involvement", Level2: [
    { Name: "Product Involvement", Level3: [
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Production (Medical Cannabis)-Category of Involvement", CriteriaEntityField: "CB CB1 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Production (Medical Cannabis)-Level of Involvement", CriteriaEntityField: "CB CB1 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Production (Medical Cannabis)-Standard Analysis", CriteriaEntityField: "CB CB1 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Production Medical Cannabis)-Category of Involvement", CriteriaEntityField: "CB CB2 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Production Medical Cannabis)-Range Ownership", CriteriaEntityField: "CB CB2 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Production Medical Cannabis)-Standard Analysis", CriteriaEntityField: "CB CB2 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Retail (Medical Cannabis)-Category of Involvement", CriteriaEntityField: "CB CB3 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Retail (Medical Cannabis)-Level of Involvement", CriteriaEntityField: "CB CB3 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Retail (Medical Cannabis)-Standard Analysis", CriteriaEntityField: "CB CB3 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Retail Medical Cannabis)-Category of Involvement", CriteriaEntityField: "CB CB4 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Retail Medical Cannabis)-Range Ownership", CriteriaEntityField: "CB CB4 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Retail Medical Cannabis)-Standard Analysis", CriteriaEntityField: "CB CB4 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Production (Recreational Cannabis)-Category of Involvement", CriteriaEntityField: "CB CB5 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Production (Recreational Cannabis)-Level of Involvement", CriteriaEntityField: "CB CB5 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Production (Recreational Cannabis)-Standard Analysis", CriteriaEntityField: "CB CB5 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Production Recreational Cannabis)-Category of Involvement", CriteriaEntityField: "CB CB6 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Production Recreational Cannabis)-Range Ownership", CriteriaEntityField: "CB CB6 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Production Recreational Cannabis)-Standard Analysis", CriteriaEntityField: "CB CB6 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Retail (Recreational Cannabis)-Category of Involvement", CriteriaEntityField: "CB CB7 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Retail (Recreational Cannabis)-Level of Involvement", CriteriaEntityField: "CB CB7 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Retail (Recreational Cannabis)-Standard Analysis", CriteriaEntityField: "CB CB7 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Recreational Cannabis)-Category of Involvement", CriteriaEntityField: "CB CB8 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Recreational Cannabis)-Range Ownership", CriteriaEntityField: "CB CB8 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Cannabis", Level4: [{ NameTemplate: "Cannabis Significant ownership (Recreational Cannabis)-Standard Analysis", CriteriaEntityField: "CB CB8 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Production-Category of Involvement", CriteriaEntityField: "OG OG1 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Production-Level of Involvement", CriteriaEntityField: "OG OG1 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Production-Standard Analysis", CriteriaEntityField: "OG OG1 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (production)-Category of Involvement", CriteriaEntityField: "OG OG2 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (production)-Range Ownership", CriteriaEntityField: "OG OG2 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (production)-Standard Analysis", CriteriaEntityField: "OG OG2 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Supporting Products/Services-Category of Involvement", CriteriaEntityField: "OG OG3 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Supporting Products/Services-Level of Involvement", CriteriaEntityField: "OG OG3 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Supporting Products/Services-Standard Analysis", CriteriaEntityField: "OG OG3 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (supporting products/services)-Category of Involvement", CriteriaEntityField: "OG OG4 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (supporting products/services)-Range Ownership", CriteriaEntityField: "OG OG4 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (supporting products/services)-Standard Analysis", CriteriaEntityField: "OG OG4 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Generation-Category of Involvement", CriteriaEntityField: "OG OG5 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Generation-Level of Involvement", CriteriaEntityField: "OG OG5 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Generation-Standard Analysis", CriteriaEntityField: "OG OG5 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (generation)-Category of Involvement", CriteriaEntityField: "OG OG6 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (generation)-Range Ownership", CriteriaEntityField: "OG OG6 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Oil & Gas", Level4: [{ NameTemplate: "Oil & Gas Significant ownership (generation)-Standard Analysis", CriteriaEntityField: "OG OG6 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Protection equipment and riot control weapons-Category of Involvement", CriteriaEntityField: "RC RC1 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Protection equipment and riot control weapons-Level of Involvement", CriteriaEntityField: "RC RC1 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Protection equipment and riot control weapons-Standard Analysis", CriteriaEntityField: "RC RC1 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Protection equipment and riot control weapons)-Category of Involvement", CriteriaEntityField: "RC RC2 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Protection equipment and riot control weapons)-Range Ownership", CriteriaEntityField: "RC RC2 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Protection equipment and riot control weapons)-Standard Analysis", CriteriaEntityField: "RC RC2 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Riot control weapons-Category of Involvement", CriteriaEntityField: "RC RC3 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Riot control weapons-Level of Involvement", CriteriaEntityField: "RC RC3 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Riot control weapons-Standard Analysis", CriteriaEntityField: "RC RC3 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Riot control weapons)-Category of Involvement", CriteriaEntityField: "RC RC4 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Riot control weapons)-Range Ownership", CriteriaEntityField: "RC RC4 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Riot control weapons)-Standard Analysis", CriteriaEntityField: "RC RC4 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Protection equipment-Category of Involvement", CriteriaEntityField: "RC RC5 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Protection equipment-Level of Involvement", CriteriaEntityField: "RC RC5 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Protection equipment-Standard Analysis", CriteriaEntityField: "RC RC5 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Protection equipment)-Category of Involvement", CriteriaEntityField: "RC RC6 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Protection equipment)-Range Ownership", CriteriaEntityField: "RC RC6 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Riot Control", Level4: [{ NameTemplate: "Riot Control Significant ownership (Protection equipment)-Standard Analysis", CriteriaEntityField: "RC RC6 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Shale Energy", Level4: [{ NameTemplate: "Shale Energy Extraction-Category of Involvement", CriteriaEntityField: "SE SE1 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Shale Energy", Level4: [{ NameTemplate: "Shale Energy Extraction-Level of Involvement", CriteriaEntityField: "SE SE1 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Shale Energy", Level4: [{ NameTemplate: "Shale Energy Extraction-Standard Analysis", CriteriaEntityField: "SE SE1 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Shale Energy", Level4: [{ NameTemplate: "Shale Energy Significant ownership (extraction)-Category of Involvement", CriteriaEntityField: "SE SE2 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Shale Energy", Level4: [{ NameTemplate: "Shale Energy Significant ownership (extraction)-Range Ownership", CriteriaEntityField: "SE SE2 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Shale Energy", Level4: [{ NameTemplate: "Shale Energy Significant ownership (extraction)-Standard Analysis", CriteriaEntityField: "SE SE2 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Whale Meat", Level4: [{ NameTemplate: "Whale Meat processing-Category of Involvement", CriteriaEntityField: "WM WM1 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Whale Meat", Level4: [{ NameTemplate: "Whale Meat processing-Level of Involvement", CriteriaEntityField: "WM WM1 Level of Involvement", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Whale Meat", Level4: [{ NameTemplate: "Whale Meat processing-Standard Analysis", CriteriaEntityField: "WM WM1 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Whale Meat", Level4: [{ NameTemplate: "Whale Meat Significant ownership (whale meat processing)-Category of Involvement", CriteriaEntityField: "WM WM2 Category of Involvement", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Whale Meat", Level4: [{ NameTemplate: "Whale Meat Significant ownership (whale meat processing)-Range Ownership", CriteriaEntityField: "WM WM2 Range Ownership", IsScreenable: true, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
		{ Name: "Whale Meat", Level4: [{ NameTemplate: "Whale Meat Significant ownership (whale meat processing)-Standard Analysis", CriteriaEntityField: "WM WM2 Standard Analysis", IsScreenable: false, CriteriaType: 4, HasFullRangeChart:false, HasOwnValue:false }] },
    ]}
]};

function uuid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
    }
    return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}

function HexToBase64(hex) {
    var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64 = "";
    var group;
    for (var i = 0; i < 30; i += 6) {
        group = parseInt(hex.substr(i, 6), 16);
        base64 += base64Digits[(group >> 18) & 0x3f];
        base64 += base64Digits[(group >> 12) & 0x3f];
        base64 += base64Digits[(group >> 6) & 0x3f];
        base64 += base64Digits[group & 0x3f];
    }
    group = parseInt(hex.substr(30, 2), 16);
    base64 += base64Digits[(group >> 2) & 0x3f];
    base64 += base64Digits[(group << 4) & 0x3f];
    base64 += "==";
    return base64;
}

function NUUID(uuid) {
    var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
    var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
    var b = hex.substr(10, 2) + hex.substr(8, 2);
    var c = hex.substr(14, 2) + hex.substr(12, 2);
    var d = hex.substr(16, 16);
    hex = a + b + c + d;
    var base64 = HexToBase64(hex);
    return new BinData(3, base64);
}

function processLevel1(criteria) {
    if (!db.CriteriaLevel1.findOne({ Name: criteria.Name })) {
        db.CriteriaLevel1.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
            ShortName: 'Products',
            Order: NumberInt(30)
        });
    }

    var criteriaLevel1Id = db.CriteriaLevel1.findOne({ Name: criteria.Name })._id;

    criteria.Level2.forEach(function (criteria, index) {
        processLevel2(criteriaLevel1Id, criteria, index);
    });
}

function processLevel2(criteriaLevel1Id, criteria, index) {
    if (!db.CriteriaLevel2.findOne({ Name: criteria.Name })) {
        db.CriteriaLevel2.insert({
            _id: NUUID(uuid()),
            CriteriaLevel1Id: criteriaLevel1Id,
            UniverseFeature: NumberInt(9),
            Name: criteria.Name,
            ShortName: 'PI',
            Order: NumberInt(1)
        });
    }

    var criteriaLevel2Id = db.CriteriaLevel2.findOne({ Name: criteria.Name })._id;
    var length = criteria.Level3.length;

    criteria.Level3.forEach(function (criteria, index) {
        processLevel3(criteriaLevel2Id, criteria, index+1);
    });
}

function processLevel3(criteriaLevel2Id, criteria, index) {
    if (!db.CriteriaLevel3.findOne({ Name: criteria.Name, CriteriaLevel2Id: criteriaLevel2Id })) 
    {
        db.CriteriaLevel3.insert({
            _id: NUUID(uuid()),
            Name: criteria.Name,
            ShortName: "",
            Order: NumberInt(index),
            CriteriaLevel2Id: criteriaLevel2Id,
            Category: "None",
			isActiveCategory: true
        });
    }

    var criteriaLevel3Id = db.CriteriaLevel3.findOne({ Name: criteria.Name, CriteriaLevel2Id: criteriaLevel2Id })._id;
    
    criteria.Level4.forEach(function (criteria) {
        processLevel4(criteriaLevel2Id, criteriaLevel3Id, criteria, index);
    });
}

function processLevel4(criteriaLevel2Id, criteriaLevel3Id, criteria, index) {
    if (!db.TemplateCriteria.findOne({ Name: criteria.NameTemplate, CriteriaLevel2Id: criteriaLevel2Id })) {
        db.TemplateCriteria.insert({
            _id: NUUID(uuid()),
            Name: criteria.NameTemplate,
            CriteriaEntityField: criteria.CriteriaEntityField,
            CriteriaType: NumberInt(criteria.CriteriaType),
            CriteriaLevel2Id: criteriaLevel2Id,
            CriteriaLevel3Id: criteriaLevel3Id,
            IsScreenable: criteria.IsScreenable,
            ResearchProducts: ["ProductInvolvement"],
            HasFullRangeChart: criteria.HasFullRangeChart==false? false : true,
			HasOwnValue: criteria.HasOwnValue==false? false : true
       });
    }
}

processLevel1(criteria); 

var criteriaLevel2Id = db.CriteriaLevel2.findOne({ Name: "Product Involvement"})._id;
var idx =0;   
//change order
var criteria3 = db.getCollection('CriteriaLevel3').find({CriteriaLevel2Id:criteriaLevel2Id}).sort({Name:1});

criteria3.forEach(function (crt)
{
	idx++;
    db.getCollection('CriteriaLevel3').update({_id: crt._id},{"$set":{Order:NumberInt(idx)}},{multi:false})
    });
	
idx=0;
var template = db.getCollection('TemplateCriteria').find({CriteriaLevel2Id:criteriaLevel2Id}).sort({CriteriaEntityField:1});

template.forEach(function (tpl)
{
	idx++;
    db.getCollection('TemplateCriteria').update({_id: tpl._id},{"$set":{Order:NumberInt(idx)}},{multi:false});
    });

db.getCollection('CriteriaLevel3').remove({Name:/Whale meat/});

db.getCollection('TemplateCriteria').remove({Name:/Whale meat/});





